package com.jic.market.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.ManagerChildApi;
import com.jic.manager.api.rest.ManagerMerchantApi;
import com.jic.manager.api.rest.ManagerStoreApi;
import com.jic.manager.request.MerchantRequest;
import com.jic.manager.response.MemStoreResponse;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.PromotionMerchantEntity;
import com.jic.market.entity.PromotionPreGoodsSku;
import com.jic.market.entity.PromotionPreGoodsSpu;
import com.jic.market.mapper.*;
import com.jic.market.request.manager.presell.*;
import com.jic.market.response.manager.presell.PresellGoodsPageQueryResponse;
import com.jic.market.response.manager.presell.PresellGoodsQueryInfo;
import com.jic.market.response.manager.presell.PresellGoodsQueryInfoDto;
import com.jic.market.response.manager.presell.PresellGoodsSkuPageQueryResponse;
import com.jic.market.service.PromotionPresellProductService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.request.GoodsPageQueryRequest;
import com.jic.product.after.response.ProductStoreInfo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预售商品表
 * @author : 
 * @email : 
 * @since : 2020-03-11 06:09:59
 * @version : v1.0.0
 */
@Slf4j
@Service
public class PromotionPresellProductServiceImpl extends MarketBaseService implements PromotionPresellProductService{

    @Autowired
    private PromotionPresellProductMapper promotionPresellProductMapper;
    @Autowired
    private PromotionMerchantEntityMapper promotionMerchantEntityMapper;
    @Resource
    private ProductListApi productListApi;

    @Autowired
    private PromotionPreGoodsSkuMapper promotionPreGoodsSkuMapper;
    @Autowired
    private PromotionPreGoodsSpuMapper promotionPreGoodsSpuMapper;
    @Autowired
    private ManagerMerchantApi managerMerchantApi;

    @Autowired
    private ManagerStoreApi managerStoreApi;

    @Autowired
    private ManagerChildApi managerChildApi;

    @Autowired
    private PromotionMerchantProductEntityMapper promotionMerchantProductEntityMapper;

    @Override
    public RestResult<Boolean> merchantAndStoreIsNull(PresellProductIsNullRequest request) {
        return executeQuery(() -> {
            PromotionPreGoodsSpu promotionPresellProductQ = new PromotionPreGoodsSpu();
            promotionPresellProductQ.setPromotionId(request.getPromotionId());
            long count = promotionPreGoodsSpuMapper.queryPageCount(promotionPresellProductQ);
            if(count == 0){
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        });
    }

    @Override
    public RestResult<Long> skuCount(PresellProductIsNullRequest request) {
        return executeQuery(() -> {
            PromotionPreGoodsSku promotionPreGoodsSku = new PromotionPreGoodsSku();
            promotionPreGoodsSku.setPromotionId(request.getPromotionId());
            return promotionPreGoodsSkuMapper.queryPageCount(promotionPreGoodsSku);
        });
    }

//    @Override
//    public RestResult<List<Long>> addBatch(PresellProductAddBatchRequest request) {
//        return executeQuery(() -> {
//            List<Long> idList = new ArrayList<>();
//            List<PresellProductAddInfo> addInfoList = request.getProductPromotionList();
//            if(null != addInfoList && !addInfoList.isEmpty()){
//                for(PresellProductAddInfo addInfo : addInfoList){
//                    PromotionPresellProduct presellProduct = new PromotionPresellProduct();
//                    BeanUtils.copyProperties(request, presellProduct, "presellProductAddInfoList");
//                    BeanUtils.copyProperties(addInfo, presellProduct);
//                    // 初始化数据
//                    presellProduct.setFrontMoney(new BigDecimal(0));
//                    presellProduct.setFrontMoneyDeductAmount(new BigDecimal(0));
//                    presellProduct.setAlreadyFrontCount(0);
//                    presellProduct.setChannelMerchantLimit(0);
//                    presellProduct.setChannelStoreLimit(0);
//                    presellProduct.setChannelIndividualLimit(0);
//                    presellProduct.setDeleteFlag(false);
//                    try{
//                        long flag = promotionPresellProductMapper.insert(presellProduct);
//                        if(flag != 0){
//                            idList.add(presellProduct.getId());
//                        }
//                    }catch (DuplicateKeyException e){
//                        // 外部重复插入数据，这个时候判断下旧数据是否为删除状态，如果是，则更新成新数据，如果不是，则不做操作
//                        PromotionPresellProduct promotionPresellProductQ = new PromotionPresellProduct();
//                        promotionPresellProductQ.setPromotionId(request.getPromotionId());
//                        promotionPresellProductQ.setMpId(addInfo.getMpId());
//                        List<PromotionPresellProduct> promotionPresellProductList = promotionPresellProductMapper.query(promotionPresellProductQ);
//                        if(null != promotionPresellProductList && !promotionPresellProductList.isEmpty()){
//                            if(promotionPresellProductList.get(0).getDeleteFlag()){
//                                presellProduct.setId(promotionPresellProductList.get(0).getId());
//                                long flag = promotionPresellProductMapper.update(presellProduct);
//                                if(flag != 0){
//                                    idList.add(presellProduct.getId());
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//            return idList;
//        });
//    }

    @Override
    public RestResult<Long> addBatch(PresellProductAddBatchRequest request) {
        return executeQuery(() -> {
            // 查询所有的商品
            GoodsPageQueryRequest goodsSkuQueryRequest = new GoodsPageQueryRequest();
            goodsSkuQueryRequest.setSpuIds(request.getSpuIds());

            List<ProductStoreInfo> goodsInfoResponseList = productListApi.queryProductBySpuId(goodsSkuQueryRequest);
//            RestResult<List<QueryGoodsInfoResponse>> restResult = goodsQueryService.queryByGoodsSpuIds(goodsSkuQueryRequest);
//
//            List<QueryGoodsInfoResponse> goodsInfoResponseList = restResult.getData();
            if(goodsInfoResponseList.isEmpty()){
                throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "没有查询到相关商品");
            }
            //迭代spu
            for(ProductStoreInfo queryGoodsInfoResponse : goodsInfoResponseList){
                PromotionPreGoodsSpu spu = new PromotionPreGoodsSpu();
                BeanUtils.copyProperties(queryGoodsInfoResponse, spu, "id");
                spu.setThirdSpuId(queryGoodsInfoResponse.getCode());
                spu.setGoodsName(queryGoodsInfoResponse.getChineseName());
                spu.setSalePriceScope(queryGoodsInfoResponse.getSalePriceWithTax().toString());
                spu.setStock(queryGoodsInfoResponse.getAvailableStock().intValue());
                spu.setPromotionId(request.getPromotionId());
                spu.setFrontPromType(request.getFrontPromType());
                spu.setGoodsSpuId(queryGoodsInfoResponse.getId());
                spu.setCreateTime(new Date());
                spu.setUpdateTime(new Date());
                spu.setDeleteFlag(false);
                // 重复数据就不管了
                try {
                    long flag = promotionPreGoodsSpuMapper.insert(spu);
                }catch (Exception e){
                    log.error("添加失败", e);

                }
                List<ProductStoreInfo> goodsSkuInfoList = queryGoodsInfoResponse.getChildren();
                for(ProductStoreInfo goodsSkuInfo : goodsSkuInfoList){
                    PromotionPreGoodsSku sku = new PromotionPreGoodsSku();
                    BeanUtils.copyProperties(goodsSkuInfo, sku, "id", "goodsSpuId");
                    sku.setPromotionId(request.getPromotionId());
                    sku.setSalePrice(goodsSkuInfo.getSalePriceWithTax());
                    sku.setFrontPromType(request.getFrontPromType());
                    sku.setGoodsSkuId(goodsSkuInfo.getId());
                    sku.setGoodsSpuId(spu.getGoodsSpuId());
                    sku.setGoodsName(goodsSkuInfo.getChineseName());
                    sku.setStock(goodsSkuInfo.getAvailableStock().intValue());
                    sku.setCreateTime(new Date());
                    sku.setUpdateTime(new Date());
                    sku.setDeleteFlag(false);
                    try{
                        long flag = promotionPreGoodsSkuMapper.insert(sku);
                    }catch (Exception e){
                        log.error("添加失败", e);
                    }
                }
            }
            return null;
        });
    }

    @Override
    public RestResult<Long> addBatchAll(PresellProductAddBatchAllRequest request) {
        return executeQuery(() -> {
            // 查询所有的商品
            GoodsPageQueryRequest goodsPageQueryRequest = new GoodsPageQueryRequest();
            //根据活动id查询出店铺ids
            List<Long> storeIds = getStoreIdsByPromotionId(request.getPromotionId());
            goodsPageQueryRequest.setStoreIds(storeIds);
//            RestResult<List<GoodsPageQueryResponse>> restResult = goodsQueryService.queryByAllCondition(goodsPageQueryRequest);
            List<ProductStoreInfo> goodsInfoResponseList = productListApi.queryProductByStoreId(goodsPageQueryRequest);
//
//            List<GoodsPageQueryResponse> goodsInfoResponseList = restResult.getData();
            if(goodsInfoResponseList.isEmpty()){
                throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "没有查询到相关商品");
            }
            //迭代spu
            for(ProductStoreInfo queryGoodsInfoResponse : goodsInfoResponseList){
                PromotionPreGoodsSpu spu = new PromotionPreGoodsSpu();
                BeanUtils.copyProperties(queryGoodsInfoResponse, spu, "id");
                spu.setPromotionId(request.getPromotionId());
                spu.setFrontPromType(request.getFrontPromType());
                spu.setGoodsSpuId(queryGoodsInfoResponse.getId());

                spu.setThirdSpuId(queryGoodsInfoResponse.getCode());
                spu.setGoodsName(queryGoodsInfoResponse.getChineseName());
                spu.setSalePriceScope(queryGoodsInfoResponse.getSalePriceWithTax().toString());
                spu.setStock(queryGoodsInfoResponse.getAvailableStock().intValue());

                spu.setCreateTime(new Date());
                spu.setUpdateTime(new Date());
                spu.setDeleteFlag(false);
                // 重复数据就不管了
                try {
                    long flag = promotionPreGoodsSpuMapper.insert(spu);
                }catch (Exception e){
                    log.error("添加失败", e);

                }
                List<ProductStoreInfo> goodsSkuInfoList = queryGoodsInfoResponse.getChildren();
                for(ProductStoreInfo goodsSkuInfo : goodsSkuInfoList){
                    PromotionPreGoodsSku sku = new PromotionPreGoodsSku();
                    BeanUtils.copyProperties(goodsSkuInfo, sku, "id", "goodsSpuId");
                    sku.setPromotionId(request.getPromotionId());
                    sku.setSalePrice(goodsSkuInfo.getSalePriceWithTax());
                    sku.setFrontPromType(request.getFrontPromType());
                    sku.setGoodsSkuId(goodsSkuInfo.getId());
                    sku.setGoodsSpuId(spu.getGoodsSpuId());
                    sku.setGoodsName(goodsSkuInfo.getChineseName());
                    sku.setStock(goodsSkuInfo.getAvailableStock().intValue());
                    sku.setCreateTime(new Date());
                    sku.setUpdateTime(new Date());
                    sku.setDeleteFlag(false);
                    try{
                        long flag = promotionPreGoodsSkuMapper.insert(sku);
                    }catch (Exception e){
                        log.error("添加失败", e);
                    }
                }
            }
            return null;
        });
    }
    private List<Long> getStoreIdsByPromotionId(Long id){
        //默认值
        List<Long> storeIds = Arrays.asList(-99L);
        if(id == null){
            return storeIds;
        }
        PromotionMerchantEntity promotionMerchantEntity = new PromotionMerchantEntity();
        promotionMerchantEntity.setPromotionId(id);
        promotionMerchantEntity.setMerchantType(2);
        promotionMerchantEntity.setDeleteFlag(0);
        List<PromotionMerchantEntity> all = promotionMerchantEntityMapper.findAll(promotionMerchantEntity);
        if(!CollectionUtils.isEmpty(all)){
            storeIds = all.stream().map(PromotionMerchantEntity::getStoreId).collect(Collectors.toList());
        }
        return storeIds;
    }
//    @Override
//    public RestResult<PageResult<PromotionPresellProduct>> pageQuery(PresellProductPageQueryRequest request) {
//        return executeQuery(() -> {
//            if(request.getStart() <= 0){
//                request.setStart(1);
//            }
//            //计算下标
//            int startIndex = (request.getStart() - 1) * request.getLimit();
//            PromotionPresellProduct promotionPresellProduct = new PromotionPresellProduct();
//            promotionPresellProduct.setPromotionId(request.getPromotionId());
//            promotionPresellProduct.setDeleteFlag(false);
//            List<PromotionPresellProduct> list = promotionPresellProductMapper.queryPage(startIndex, request.getLimit(), promotionPresellProduct);
//            long count = promotionPresellProductMapper.queryPageCount(promotionPresellProduct);
//            PageResult pageResult = new PageResult();
//            pageResult.setRows(list);
//            pageResult.setTotal(count);
//            return pageResult;
//        });
//    }

    @Override
    public RestResult<PageResult<PresellGoodsPageQueryResponse>> pageQueryNew(PresellGoodsPageQueryRequest request) {
        return executeQuery(() -> {
            if(request.getStart() <= 0){
                request.setStart(1);
            }
            int startIndex = (request.getStart() - 1) * request.getLimit();
            List<PresellGoodsQueryInfoDto> infoDtoList = promotionPreGoodsSpuMapper.queryPageNew(startIndex, request.getLimit(), request);
            long total = promotionPreGoodsSpuMapper.queryPageCountNew(request);
            List<PresellGoodsPageQueryResponse> responseList = getGoodsPageQueryResponses(infoDtoList);
            PageResult pageResult = new PageResult();
            pageResult.setRows(responseList);
            pageResult.setTotal(total);
            return pageResult;
        });
    }

    @Override
    public RestResult<PageResult<PresellGoodsPageQueryResponse>> pageQueryNewNew(PresellGoodsPageQueryRequest request) {
        return executeQuery(() -> {
            int startIndex = (request.getStart() - 1) * request.getLimit();
            List<PromotionPreGoodsSpu> promotionPreGoodsSpuList = promotionPreGoodsSpuMapper.queryPageNewNew(startIndex, request.getLimit(), request);
            long total = promotionPreGoodsSpuMapper.queryPageCountNewNew(request);
            List<PresellGoodsPageQueryResponse> presellGoodsPageQueryResponseList = new ArrayList<>();
            if(null != promotionPreGoodsSpuList && !promotionPreGoodsSpuList.isEmpty()){
                for(PromotionPreGoodsSpu promotionPreGoodsSpu : promotionPreGoodsSpuList){
                    PresellGoodsPageQueryResponse presellGoodsPageQueryResponse = new PresellGoodsPageQueryResponse();
                    PresellGoodsQueryInfo presellGoodsQueryInfo = new PresellGoodsQueryInfo();
                    presellGoodsQueryInfo.setId(promotionPreGoodsSpu.getGoodsSpuId());
                    presellGoodsQueryInfo.setDataType("SPU");
                    List<Long> goodsSpuIds = new ArrayList<>();
                    goodsSpuIds.add(presellGoodsQueryInfo.getId());
//                    RestResult<List<GoodsSpuPageQueryResponse>> restResult = goodsQueryService.querySpu(goodsSpuIds);
                    List<ProductStoreInfo> productStoreInfos = productListApi.queryProductBySpuId(new GoodsPageQueryRequest() {{
                        setSpuIds(goodsSpuIds);
                    }});
                    ProductStoreInfo productStoreInfo = productStoreInfos.get(0);

                    presellGoodsQueryInfo.setSalePriceScope(productStoreInfo.getSalePriceWithTax().toString());
                    presellGoodsQueryInfo.setThirdId(productStoreInfo.getCode());
                    presellGoodsQueryInfo.setGoodsName(productStoreInfo.getChineseName());
                    presellGoodsQueryInfo.setGoodsCategory(productStoreInfo.getCategoryName());
                    presellGoodsQueryInfo.setStock(productStoreInfo.getAvailableStock().intValue());
                    presellGoodsQueryInfo.setStoreId(productStoreInfo.getStoreId());
                    presellGoodsQueryInfo.setStoreName(productStoreInfo.getStoreName());
                    presellGoodsQueryInfo.setMerchantId(productStoreInfo.getMerchantId());
                    presellGoodsQueryInfo.setMerchantName(productStoreInfo.getMerchantName());
//                    if(restResult.getCode().equals("0000") && null != restResult.getData()
//                        && !restResult.getData().isEmpty()){
//                        BeanUtils.copyProperties(restResult.getData().get(0), presellGoodsQueryInfo,"id");
//                    }
                    presellGoodsPageQueryResponse.setGoodsInfo(presellGoodsQueryInfo);
                    // sku
                    PromotionPreGoodsSku promotionPreGoodsSkuQ = new PromotionPreGoodsSku();
                    promotionPreGoodsSkuQ.setPromotionId(request.getPromotionId());
                    promotionPreGoodsSkuQ.setGoodsSpuId(promotionPreGoodsSpu.getGoodsSpuId());
                    List<PromotionPreGoodsSku> promotionPreGoodsSkuList = promotionPreGoodsSkuMapper.query(promotionPreGoodsSkuQ);
                    List<PresellGoodsSkuPageQueryResponse> skuInfoList = new ArrayList<>();
                    if(null != promotionPreGoodsSkuList && !promotionPreGoodsSkuList.isEmpty()){
                        for(PromotionPreGoodsSku promotionPreGoodsSku: promotionPreGoodsSkuList){
                            PresellGoodsSkuPageQueryResponse presellGoodsSkuPageQueryResponse = new PresellGoodsSkuPageQueryResponse();
                            PresellGoodsQueryInfo presellGoodsQueryInfoSku = new PresellGoodsQueryInfo();
                            BeanUtils.copyProperties(promotionPreGoodsSku, presellGoodsQueryInfoSku);
                            presellGoodsQueryInfoSku.setId(promotionPreGoodsSku.getGoodsSkuId());
                            presellGoodsQueryInfoSku.setSalePriceScope(promotionPreGoodsSku.getSalePrice().toString());
                            presellGoodsQueryInfoSku.setDataType("SKU");
                            List<Long> skuIds = new ArrayList<>();
                            skuIds.add(promotionPreGoodsSku.getGoodsSkuId());
//                            RestResult<List<GoodsSkuQueryResponse>> restResultSku = goodsQueryService.querySku(skuIds);
//                            if(restResultSku.getCode().equals("0000") && null != restResultSku.getData()
//                                    && !restResultSku.getData().isEmpty()){
//                                BeanUtils.copyProperties(restResultSku.getData().get(0), presellGoodsQueryInfoSku,"id");
//                            }
                            List<ProductStoreInfo> skuList = productListApi.queryProductBySpuId(new GoodsPageQueryRequest() {{
                                setSpuIds(skuIds);
                            }});
                            ProductStoreInfo skuInfo = skuList.get(0);

                            presellGoodsQueryInfoSku.setSalePriceScope(skuInfo.getSalePriceWithTax().toString());
                            presellGoodsQueryInfoSku.setThirdId(skuInfo.getCode());
                            presellGoodsQueryInfoSku.setGoodsName(skuInfo.getChineseName());
                            presellGoodsQueryInfoSku.setGoodsCategory(skuInfo.getCategoryName());
                            presellGoodsQueryInfoSku.setStock(skuInfo.getAvailableStock().intValue());
                            presellGoodsQueryInfoSku.setStoreId(skuInfo.getStoreId());
                            presellGoodsQueryInfoSku.setStoreName(skuInfo.getStoreName());
                            presellGoodsQueryInfoSku.setMerchantId(skuInfo.getMerchantId());
                            presellGoodsQueryInfoSku.setMerchantName(skuInfo.getMerchantName());

                            presellGoodsSkuPageQueryResponse.setGoodsInfo(presellGoodsQueryInfoSku);
                            skuInfoList.add(presellGoodsSkuPageQueryResponse);
                            presellGoodsPageQueryResponse.setSkuInfoList(skuInfoList);
                        }
                    }
                    presellGoodsPageQueryResponseList.add(presellGoodsPageQueryResponse);
                }
            }
            PageResult pageResult = new PageResult();
            pageResult.setRows(presellGoodsPageQueryResponseList);
            pageResult.setTotal(total);
            return pageResult;
        });
    }

    private List<PresellGoodsPageQueryResponse> getGoodsPageQueryResponses(List<PresellGoodsQueryInfoDto> infoDtoList) {
        List<PresellGoodsPageQueryResponse> responseList = new ArrayList<>();
        if(null != infoDtoList && !infoDtoList.isEmpty()){
            // 思路：由于返回的数据都是按照spuId进行排序进行的，所以这里依次迭代数据，如果下一个spu与上一个spu不等，则说明当前spu已经结束
            Long preGoodsSpuId = null;
            for(PresellGoodsQueryInfoDto infoDto : infoDtoList){
                if(null == preGoodsSpuId || !infoDto.getGoodsSpuId().equals(preGoodsSpuId)){
                    // 赋值，注意，这里是在if内赋值，在外面赋值就进不来了
                    preGoodsSpuId = infoDto.getGoodsSpuId();

                    PresellGoodsPageQueryResponse response = new PresellGoodsPageQueryResponse();
                    // spu信息
                    PresellGoodsQueryInfo spuInfo = new PresellGoodsQueryInfo();
                    BeanUtils.copyProperties(infoDto, spuInfo, "goodsSpuId");
                    spuInfo.setDataType("SPU");
                    spuInfo.setId(infoDto.getGoodsSpuId());
                    spuInfo.setThirdId(infoDto.getThirdSpuId());
                    spuInfo.setGoodsName(infoDto.getSpuGoodsName());
                    spuInfo.setGoodsCategory(infoDto.getSpuGoodsCategory());
                    spuInfo.setMeasureUnit(infoDto.getSpuMeasureUnit());
                    spuInfo.setSalePriceScope(infoDto.getSalePriceScope());
                    spuInfo.setStock(infoDto.getSpuStock());
                    spuInfo.setGoodsAttributes(infoDto.getSpuGoodsAttributes());
                    spuInfo.setGoodsStatus(infoDto.getSpuGoodsStatus());
                    spuInfo.setChildId(infoDto.getChildId());
                    spuInfo.setStoreId(infoDto.getStoreId());
                    spuInfo.setMerchantId(infoDto.getMerchantId());
                    // 商家店铺平台名称，后续可以改成每次查出来然后放入缓存，这样提升效率
                    try{
                        spuInfo.setMerchantName(getMerchanName(infoDto.getMerchantId()));
                        spuInfo.setStoreName(getStoreName(infoDto.getStoreId()));
                        spuInfo.setChildName(getChildName(infoDto.getChildId()));
                    }catch (Exception e){
                        log.error("远程feign异常");
                    }
                    response.setGoodsInfo(spuInfo);
                    // sku信息
                    List<PresellGoodsSkuPageQueryResponse> skuInfoList = new ArrayList<>();
                    PresellGoodsQueryInfo skuInfo = new PresellGoodsQueryInfo();
                    BeanUtils.copyProperties(infoDto, skuInfo, "goodsSkuId");
                    skuInfo.setDataType("SKU");
                    skuInfo.setId(infoDto.getGoodsSkuId());
                    skuInfo.setThirdId(infoDto.getThirdSkuId());
                    skuInfo.setGoodsName(infoDto.getSkuGoodsName());
                    skuInfo.setGoodsCategory(infoDto.getSkuGoodsCategory());
                    skuInfo.setMeasureUnit(infoDto.getSkuMeasureUnit());
                    skuInfo.setSalePriceScope(infoDto.getSalePrice().toString());
                    skuInfo.setStock(infoDto.getSkuStock());
                    skuInfo.setGoodsAttributes(infoDto.getSkuGoodsAttributes());
                    skuInfo.setGoodsStatus(infoDto.getSkuGoodsStatus());
                    skuInfo.setChildId(spuInfo.getChildId());
                    skuInfo.setStoreId(spuInfo.getStoreId());
                    skuInfo.setMerchantId(spuInfo.getMerchantId());
                    skuInfo.setChildName(spuInfo.getChildName());
                    skuInfo.setMerchantName(spuInfo.getMerchantName());
                    skuInfo.setStoreName(spuInfo.getStoreName());
                    PresellGoodsSkuPageQueryResponse goodsSkuPageQueryResponse = new PresellGoodsSkuPageQueryResponse();
                    goodsSkuPageQueryResponse.setGoodsInfo(skuInfo);
                    skuInfoList.add(goodsSkuPageQueryResponse);
                    response.setSkuInfoList(skuInfoList);
                    responseList.add(response);
                }else {
                    // 赋值
                    preGoodsSpuId = infoDto.getGoodsSpuId();

                    // 找到已经赋值好的spuResponse对象
                    for(PresellGoodsPageQueryResponse spuResponse : responseList){
                        if(infoDto.getGoodsSpuId().equals(spuResponse.getGoodsInfo().getId())){
                            // sku信息
                            PresellGoodsQueryInfo skuInfo = new PresellGoodsQueryInfo();
                            BeanUtils.copyProperties(infoDto, skuInfo, "goodsSkuId");
                            skuInfo.setDataType("SKU");
                            skuInfo.setId(infoDto.getGoodsSkuId());
                            skuInfo.setThirdId(infoDto.getThirdSkuId());
                            skuInfo.setGoodsName(infoDto.getSkuGoodsName());
                            skuInfo.setGoodsCategory(infoDto.getSkuGoodsCategory());
                            skuInfo.setMeasureUnit(infoDto.getSkuMeasureUnit());
                            skuInfo.setSalePriceScope(infoDto.getSalePrice().toString());
                            skuInfo.setStock(infoDto.getSkuStock());
                            skuInfo.setGoodsAttributes(infoDto.getSkuGoodsAttributes());
                            skuInfo.setGoodsStatus(infoDto.getSkuGoodsStatus());
                            skuInfo.setChildId(spuResponse.getGoodsInfo().getChildId());
                            skuInfo.setStoreId(spuResponse.getGoodsInfo().getStoreId());
                            skuInfo.setMerchantId(spuResponse.getGoodsInfo().getMerchantId());
                            skuInfo.setChildName(spuResponse.getGoodsInfo().getChildName());
                            skuInfo.setMerchantName(spuResponse.getGoodsInfo().getMerchantName());
                            skuInfo.setStoreName(spuResponse.getGoodsInfo().getStoreName());
                            PresellGoodsSkuPageQueryResponse goodsSkuPageQueryResponse = new PresellGoodsSkuPageQueryResponse();
                            goodsSkuPageQueryResponse.setGoodsInfo(skuInfo);
                            spuResponse.getSkuInfoList().add(goodsSkuPageQueryResponse);
                            break;
                        }
                    }
                }
            }
        }
        return responseList;
    }
    private String getChildName(Long childId) {
        RestResult<String> result = managerChildApi.queryChildNameById(childId);
        if(null != result){
            return result.getData();
        }
        return null;
    }

    private String getMerchanName(Long merchantId){
        List<Long> merchantList = new ArrayList<>();
        merchantList.add(merchantId);
        RestResult<List<MerchantRequest>> resultList = managerMerchantApi.queryMerchantListByid(merchantList);
        if(null != resultList && null != resultList.getData() && !resultList.getData().isEmpty()){
            return resultList.getData().get(0).getName();
        }
        return null;
    }

    private String getStoreName(Long storeId){
        RestResult<MemStoreResponse> result = managerStoreApi.getStoreInfo(storeId);
        if(null != result && null != result.getData()){
            return result.getData().getStoreName();
        }
        return null;
    }
//    //TODO dubbo升级
//    private String getStoreName(Long storeId){
//        Mestore result = managerStoreApi.queryByPrimaryKey(storeId);
//        if(null != result){
//            return result.getStoreName();
//        }
//        return null;
//    }


    @Override
    public RestResult<Long> delBatch(PresellProductDeleteRequest request) {
        return executeQuery(() -> {
            //
            if(null == request.getPromotionId() || 0 == request.getPromotionId()){
                throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "活动id必须传");
            }

            List<Long> needDeleteGoodsSkuIds = new ArrayList<>();
            if(null != request.getGoodsSkuIdList() && !request.getGoodsSkuIdList().isEmpty()){
                needDeleteGoodsSkuIds.addAll(request.getGoodsSkuIdList());
            }

            // 根据spuList来删除
            if(null != request.getGoodsSpuIdList() && !request.getGoodsSpuIdList().isEmpty()){
                // 查询所有的spu下面的skuIds
                List<Long> goodsSkuIds = promotionPreGoodsSkuMapper.querySkuIdsBySpuIds(request.getPromotionId(), request.getGoodsSpuIdList());
                if(null != goodsSkuIds && !goodsSkuIds.isEmpty()){
                    needDeleteGoodsSkuIds.addAll(goodsSkuIds);
                }
                promotionPreGoodsSpuMapper.deleteBatchByGoodsSpuId(request.getPromotionId(), request.getGoodsSpuIdList());
            }


            // 根据skuList来删除
            if(null != needDeleteGoodsSkuIds && !needDeleteGoodsSkuIds.isEmpty()){
                long flag = promotionPreGoodsSkuMapper.deleteBatchByGoodsSkuId(request.getPromotionId(), needDeleteGoodsSkuIds);
            }
            // 把对应的spu下没有商品的删除，
            List<Long> goodsSpuIds = promotionPreGoodsSpuMapper.queryGoodsSpuIdsIsNoSku(request.getPromotionId());
            if(null != goodsSpuIds && !goodsSpuIds.isEmpty()){
                promotionPreGoodsSpuMapper.deleteNoSku(request.getPromotionId(), goodsSpuIds);
            }
            return 0L;
        });
    }

    @Override
    public RestResult<Long> update(PresellProductUpdateRequest request){
        return executeQuery(() -> {
            PromotionPreGoodsSku presellProduct = new PromotionPreGoodsSku();
            BeanUtils.copyProperties(request, presellProduct, "id");
            presellProduct.setGoodsSkuId(request.getId());
            return promotionPreGoodsSkuMapper.updateByGoodsSkuId(presellProduct);
        });
    }


}